package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.scyx.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.IamFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.scyx.OverhaulProjectMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.Constant;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.scyx.OverhaulProject;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.scyx.OverhaulProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by yangsy on 2016/11/17.
 */
public class OverhaulProjectServiceImpl implements OverhaulProjectService {
    @Autowired
    private OverhaulProjectMapper overhaulProjectMapper ;
    @Autowired
    private IamFileMapper iamFileMapper;

    @Override
    public int deleteByPrimaryKey(String overhaulProjectId) {
        return overhaulProjectMapper.deleteByPrimaryKey(overhaulProjectId);
    }

    @Override
    public int insert(OverhaulProject record) {
        return overhaulProjectMapper.insert(record);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(OverhaulProject record,String uploadFiles,String deletedFiles) {
        //插入数据成功之后，返回的条数
        int resultCount = 0;
        //保存业务数据
        record.setOverhaulProjectId(UniqueUtil.uuid()); // 设置主键
        record.setCreateTime(new Date()); // 设置创建时间
        record.setIsDelete(Constant.DEFAULT); // 新增标识
        resultCount =  overhaulProjectMapper.insertSelective(record);
        //更新的附件不为空的情况
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getOverhaulProjectId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        // TODO: 2017/1/4
        //删除的附件不为空的情况
        if(deletedFiles!=null && !deletedFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
        }
        return resultCount;
    }

    @Override
    public int insertSelective(OverhaulProject record) {
        return overhaulProjectMapper.insertSelective(record);
    }

    @Override
    public OverhaulProject selectByPrimaryKey(String overhaulProjectId) {
        return overhaulProjectMapper.selectByPrimaryKey(overhaulProjectId);
    }

    @Override
    public int updateByPrimaryKeySelective(OverhaulProject record) {
        return overhaulProjectMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(OverhaulProject record, String uploadFiles, String deletedFiles) {
        //插入数据成功之后，返回的条数
        int resultCount = 0;
        //
        OverhaulProject overhaulProject = overhaulProjectMapper.selectByPrimaryKey(record.getOverhaulProjectId());
        //  设置大修项目名称
        overhaulProject.setOverhaulProjectName(record.getOverhaulProjectName());
        //  设置备注
        overhaulProject.setRemark(record.getRemark());
        //  修改时间
        overhaulProject.setModifyTime(new Date());
        resultCount += overhaulProjectMapper.updateByPrimaryKeySelective(record);
        //更新附件信息
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getOverhaulProjectId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        //需要删除的附件信息
        if(deletedFiles!=null && !deletedFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    iamFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return resultCount;
    }

    @Override
    public int updateByPrimaryKey(OverhaulProject record) {
        return overhaulProjectMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(OverhaulProject overhaulProject) {
        JSONObject jsonObject = new JSONObject();
        overhaulProject.setLimit( overhaulProject.getLimit() == null ? 10 : overhaulProject.getLimit() );
        overhaulProject.setOffset( overhaulProject.getOffset() == null ? 0 : overhaulProject.getOffset() );
        jsonObject.put("total",overhaulProjectMapper.queryCount(overhaulProject));
        jsonObject.put("rows", JSON.toJSON(overhaulProjectMapper.queryList(overhaulProject)));
        return jsonObject;
    }
}
